package fr.miage.orleans.projetindus2014;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import fr.miage.orleans.projetindus2014.utils.ListeAdapter;
import fr.miage.orleans.projetindus2014.utils.ListeData;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private EditText champ = null;
	private Button search = null;
	private ListView list = null;	
	private ArrayList<ListeData> ld;
	private ListeAdapter la;
	final String EXTRA_DETAILS = "details";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        champ = (EditText)findViewById(R.id.textMain);
        search = (Button)findViewById(R.id.buttonMain);
        ld = new ArrayList<ListeData>();
        la = new ListeAdapter(ld, this);
        list = (ListView)findViewById(R.id.listMain);
        list.setAdapter(la);
        search.setOnClickListener(new View.OnClickListener() {
		
        	@Override
        	public void onClick(View v) {
        		if(!(champ.getText().toString().equals(""))){
        			ld.add(0, new ListeData(champ.getText().toString(), "Cote de : "+champ.getText().toString(), "Date de : "+champ.getText().toString(), "Lieu de : "+champ.getText().toString()));
        			la.notifyDataSetChanged();
        			champ.setText("");
        		}else{
        			Context context = getApplicationContext();
        			CharSequence text = "La zone de recherche est vide";
        			int duration = Toast.LENGTH_SHORT;
        			Toast toast = Toast.makeText(context, text, duration);
        			toast.show();
        		}
			}
		});
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				String details = ld.get(arg2).getTitle();
				Intent intent = new Intent(MainActivity.this, DetailsActivty.class);
				intent.putExtra(EXTRA_DETAILS, details);
				startActivity(intent);
			}
		});
        //setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
        
    
}
